/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.heathrowswingclient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import cz.muni.fi.pa165.heathrowswingclient.dto.AircraftDTO;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Martin Zitnik
 */
public class AircraftTableModel extends AbstractTableModel {

    private Client client = Client.create();
    private WebResource webResource = client.resource("http://localhost:8080/pa165/webresources/");
    private List<AircraftDTO> aircrafts = new ArrayList<AircraftDTO>();

    public AircraftTableModel() {
        loadAircrafts();
    }

    public void loadAircrafts() {
        ClientResponse clientResponse = webResource.path("aircraft").accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

        if (clientResponse.getStatus() != 200) {
            System.out.println(clientResponse.getLocation());
            throw new RuntimeException("error connecting: " + clientResponse.getStatus());
        }

        String output = clientResponse.getEntity(String.class);
        java.lang.reflect.Type type = new TypeToken<List<AircraftDTO>>() {
}.getType();
        List<AircraftDTO> jsonAircrafts = new Gson().fromJson(output, type);

        aircrafts.clear();

        for (AircraftDTO a : jsonAircrafts) {
            aircrafts.add(a);
        }
        fireTableDataChanged();
    }

    public void addAircraft(AircraftDTO aircraft) {
        String json = new Gson().toJson(aircraft);
        ClientResponse response = webResource.path("aircraft").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, json);
        if (response.getStatus() != 200) {
            throw new RuntimeException("connection failed" + response.getStatus());
        }
        ClientResponse response1 = webResource.path("aircraft").path("code").path(aircraft.getCode()).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
        String air = response1.getEntity(String.class);
        java.lang.reflect.Type type = new TypeToken<AircraftDTO>() {}.getType();
        aircraft = new Gson().fromJson(air, type);
        
        aircrafts.add(aircraft);
        fireTableDataChanged();
    }

    public AircraftDTO getRowElement(int i) {
        return aircrafts.get(i);
    }

    public void deleteAircraft(int rowindex) {
        String id = String.valueOf(getRowElement(rowindex).getId());
        AircraftDTO delete = aircrafts.get(rowindex);
        ClientResponse response = webResource.path("aircraft").path(id)
            .type(MediaType.TEXT_PLAIN).delete(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("connection failed" + response.getStatus());
        }
        aircrafts.remove(rowindex);
        fireTableDataChanged();
    }
    
    public void updateAircraft(AircraftDTO air){
        String json = new Gson().toJson(air);
        ClientResponse response = webResource.path("aircraft").type(MediaType.APPLICATION_JSON).put(ClientResponse.class, json);
        fireTableDataChanged();
    }

    public int getRowCount() {
        return aircrafts.size();
    }

    public int getColumnCount() {
        return 3;
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
            return "Code";
        case 1:
            return "Type";
        case 2:
            return "Capacity";
        default:
            throw new IllegalArgumentException("columnIndex");
    }
}

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex >= aircrafts.size() && rowIndex < 0) {
            throw new IllegalArgumentException();
        }

        switch (columnIndex) {
            case 0:
                return aircrafts.get(rowIndex).getCode();
            case 1:
                return aircrafts.get(rowIndex).getType();
            case 2:
                return aircrafts.get(rowIndex).getCapacity();
            default:
                throw new IllegalArgumentException();
        }
    }
}
